package com.tedu._06weibo.controller;

import com.tedu._06weibo.mapper.CommentMapper;
import com.tedu._06weibo.pojo.dto.CommentDTO;
import com.tedu._06weibo.pojo.entity.Comment;
import com.tedu._06weibo.pojo.entity.User;
import com.tedu._06weibo.pojo.vo.CommentVO;
import com.tedu._06weibo.result.JsonResult;
import com.tedu._06weibo.result.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@Api(tags = "评论模块")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;
    @PostMapping("/v1/comment/insert")
    @ApiOperation("发表评论")
    public JsonResult insert(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session){
        User user = (User) session.getAttribute("user");
        if (user!=null){
            System.out.println(commentDTO);
            Comment comment = new Comment();
            BeanUtils.copyProperties(commentDTO,comment);
            comment.setCreated(new Date());
            comment.setUserId(user.getId());
            commentMapper.insert(comment);
            return JsonResult.success();
        }
        return new JsonResult(Status.NOT_LOGIN);
    }

    @GetMapping("v1/comment/selectByWeiboId")
    @ApiOperation("查看评论")
    @ApiImplicitParam(value = "微博ID",name = "id",required = true,example = "1")
    public JsonResult selectByweiboId(Integer id){
        return JsonResult.success(commentMapper.selectByweiboId(id));
    }

    @GetMapping("/selectByWeiboAndUserId")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "微博ID", name = "weiboId", required = true, example = "1"),
            @ApiImplicitParam(value = "用户ID", name = "userId", required = true, example = "1")
    })
    public JsonResult findByWeiboAndUserId(Integer weiboId,Integer userId){
        return JsonResult.success();
    }
}
